package cn.itcast.controller.system;

import cn.itcast.base.BaseController;
import cn.itcast.domain.role.Role;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.user.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.commons.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/system/user")
public class UserController extends BaseController {
    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list", name = "用户的分页查询")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        PageInfo pageInfo = userService.findAll(pageNum, pageSize, companyId);
        request.setAttribute("page", pageInfo);
        return "system/user/user-list";
    }



    @RequestMapping(value = "/edit", name = "用户的新增")
    public String edit(User user) {
        //设置企业标识
        user.setCompanyId(companyId);
        //设置企业名称
        user.setCompanyName(companyName);
        if (UtilFuns.isEmpty(user.getId())) {
            //设置 主键
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            user.setId(uuid);
            userService.save(user);
        } else {
            userService.update(user);

            //到最新页面展示数据

        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id) {
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }


  /*  @RequestMapping(value = "/toUpdate", name = "页面编辑")
    public String toUpdate(String id) {
        //获取要修改的用户
        User user = userService.findById(id);
        List<Dept> list = deptService.findAllDept(companyId);
        request.setAttribute("deptList", list);
        request.setAttribute("user", user);
        return "system/user/user-update";
    }*/
    @RequestMapping(value = "/toUpdate",name = "页面编辑")
    public String toUpdate(String id) {
        //获取要修改的用户
        User user = userService.findById(id);
        List<Dept> list = deptService.findAllDept(companyId);
        request.setAttribute("deptList",list);
        request.setAttribute("user",user);
        return "system/user/user-update";
    }



    @RequestMapping(value = "/toAdd",name = "用户的新增页面")
    public String toAdd() {
        //获取所有部门
        List<Dept> deptList = deptService.findAllDept(companyId);
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/roleList",name = "用户的角色选取界面")
    public String roleList(String id) {
        //根据用户id获取用户到页面展示用户名称
        User user = userService.findById(id);
        //获取所有角色到页面展示角色
       List<Role> roleList =  roleService.findAllRole(companyId);
        //获取当前用户具备的角色
        List<Role> userRoleList = roleService.findUserRoleList(id);
        //对查出来的用户角色进行遍历循环 组装角色id（到页面和所有角色匹配）
        String str = "";
        for (Role role : userRoleList) {
            str += role.getId()+",";// "1,2,3,4,"
        }
        request.setAttribute("user",user);
        request.setAttribute("roleList",roleList);
        request.setAttribute("userRoleList",userRoleList);
        //到用户选取角色页面
        return "system/user/user-role";
    }
    @RequestMapping(value = "/changeRole",name = "用户的角色添加")
    public String changeRole(String userid,String[] roleIds) {
        roleService.changeRole(userid, roleIds);
        //到最新页面展示最新数据
        return "redirect:/syste/user/list.do";
    }

    //用户修改信息

    @RequestMapping(value = "/information",name = "修改登录用户信息页面")
    public String information(){
        User email = userService.findByEmail(user.getEmail());
        request.setAttribute("user",email);

        List<Dept> list = deptService.findAllDept(companyId);
        //System.out.println(list.toString());
        request.setAttribute("deptList",list);

        return "system/user/user-information";
    }

    @RequestMapping(value = "/userEdit",name = "登录用户信息修改")
    public String userEdit(User user) {
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);

        String password = user.getPassword();
        String email = user.getEmail();
        String md5 = new Md5Hash(password, email, 1).toString();

        user.setPassword(md5);
        userService.update(user);
        return "redirect:/logout.do";
    }
}
